#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/statvfs.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include <sched.h>
#include <ctype.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <linux/if.h>

#define DF_FILE  "/tmp/df.txt"

struct df_info {
    char * file_system;
    char * size;
    char * used;
    char * available;
    char * use_percent;
    char * mounted;
};

int xy_usb_detect_mount(void)
{
    FILE * fp;
    char buf[1024] = {0};
    int i = 0;
    char * line = NULL;
    size_t len = 0;
    size_t read = 0;
    struct df_info * info;
   
    info = (struct df_info *) malloc (sizeof(struct df_info));
    info->file_system = (char *) malloc (64 * sizeof (char));
    info->size = (char *) malloc (16 * sizeof (char));
    info->used = (char *) malloc (16 * sizeof(char));
    info->available = (char *) malloc (16 * sizeof(char));
    info->use_percent = (char *) malloc (16 * sizeof(char));
    info->mounted = (char *) malloc (32 * sizeof(char)); 

    strcpy(info->size, "512M");
    printf("size = %s\n", info->size);

#if 1
    if (!system("df -h > /tmp/df.txt")) {
        if ((fp = fopen(DF_FILE, "r")) == NULL) {
            printf("%s failed to open.\n", (char *)DF_FILE);
            return -1;
        }


        while (fgets(buf, sizeof buf, fp)) {
            memset(info, 0, sizeof(struct df_info));
            printf("%s   %s   %s   %s   %s   %s\n", info->file_system, info->size, info->used, info->available, info->use_percent, info->mounted);
            sscanf(buf, "%s %s %s %s %s %s", info->file_system, info->size, info->used, info->available, info->use_percent, info->mounted);

            printf("%s   %s   %s   %s   %s   %s\n", info->file_system, info->size, info->used, info->available, info->use_percent, info->mounted);

            if (!strncmp(info->file_system, "/dev/sd", 7)) {
//                printf("%s   %s   %s   %s   %s   %s\n", info->file_system, info->size,    info->used, info->available, info->use_percent, info->mounted);
            }
        }

        fclose(fp);
    }
#endif
    return 0;
}

void main()
{
    while (1) {
        xy_usb_detect_mount();
        sleep(2);
    }
}
